function addMethod(object, name, func) {
    var old = object[name];
    object[name] = function() {
        if (func.length == arguments.length)
             return func.apply( this, arguments);
         else if (typeof old == 'function')
            return old.apply(this, arguments);
     };
}
var sends = null;

function methods() {
    this.response = null;

    this.groupMethods = function() {
        sends = new Array();
    };

    this.execMethods = function() {
        this.ajax = new Ajax();
        this.ajax.parent = this;

        var snd = encodeURIComponent(sends.join(','));
        sends = null;
        this.ajax.execPOST('execMethods=' + snd);
        this.ajax.onresponse = function() {
            this.parent.response = this.response;
            this.parent.onresponse();
        }
    };
    
    this.onrespose = function() {
    
    };
}

var gMethods = new methods();

<%@ include file="clases.jsp"%>
<%@page import="com.google.gson.*" %>
<%@page import="java.lang.reflect.*;" %>
<%
	response.setCharacterEncoding("UTF-8");
	int nc = clases.length, c;

	for (c = 0; c < nc; c ++) {
		String clase = clases[c];
%>
		function <%= clase %>() {
            this.response = null;
			this.ajax = new Ajax();
			this.ajax.parent = this;

            this.ajax.onresponse = function() {
                this.parent.response = this.response;
                this.parent.onresponse();
            }
			
			this.onresponse = function() {
				
			};
<%		
			Class<?> jclase = Class.forName("managers." + clase);
			Method[] metodos = jclase.getDeclaredMethods();
			
			for (int m = 0; m < metodos.length; m ++) {
				Method metodo = metodos[m];
				Class<?>[] params = metodo.getParameterTypes();
				
				String prms = "";
				String tprms = "";
				for (int p = 0; p < params.length; p ++) {
					prms += "p" + p + ", ";
					
					if (tprms.equals("")) tprms += "'";
					tprms += params[p].getSimpleName() + "='+p" + p + "+'&";  
				}
				if (prms.length() > 0) prms = prms.substring(0, prms.length() - 2);
				if (tprms.length() > 0) tprms = tprms.substring(0, tprms.length() - 3);
%>
				addMethod(this, '<%= metodo.getName() %>', function(<%= prms %>) {
					var prms = 'className=<%= clase %>&method=<%= metodo.getName() %>';
					prms += '&params='<% if (tprms.length() > 0) { %>+encodeURIComponent(<%= tprms %>) <% } %>;
					
					//alert(prms);
                    
                    if (sends == null) this.ajax.execPOST(prms);
                    else sends.push(prms);
				});
<%				
			}
%>		
		}
<%		
	}

%>